import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';

import 'package:schoollifedemo/base/routers.dart';

class Application {
  // 工厂方法制作单例
  static late final Application _application = Application._internal();

  Application._internal();

  factory Application() => _application;

  final String _appVersion = '1.0.0';
  String? _loginAccount;
  FluroRouter? _router;

  void init() {
    _router = FluroRouter();
    defineRoutes(_router);
  }

  String getAppVersion() => _appVersion;

  void dispose() {
    _router = null;
  }

  FluroRouter? getRouter() {
    return _router;
  }

  // 传送中文字符串时，会导致错误
  Future<dynamic> routerNavigateTo(
    BuildContext context,
    String path, {
    Map<String, dynamic>? params,
    bool replace = false,
    bool clearStack = false,
    TransitionType? transition,
    Duration? transitionDuration,
  }) async {
    String realPath = path;
    if (params != null && params.isNotEmpty) {
      realPath = realPath + '?';
      params.forEach((key, value) {
        realPath = realPath + '$key=$value&';
      });
    }
    return await _router?.navigateTo(
      context,
      realPath,
      replace: replace,
      clearStack: clearStack,
      transition: transition,
      transitionDuration: transitionDuration,
    );
  }

  Future<dynamic> routerPop(BuildContext context, [dynamic result]) async {
    return _router?.pop(context, result);
  }

  void setLoginAccount(String account) {
    _loginAccount = account;
  }

  String? getLoginAccount() => _loginAccount;
}
